import {
	apiBaseUrl
} from './config.js'

import Db from "./db.js"

let ajax_num = 0;
const ajax = (url, method, data, data_methods) => {
	return new Promise((resolve, reject) => {
		let _data = data;
		if (data_methods) {
			// 判断是否需要登录
			if (methodsToken.indexOf(data_methods) >= 0) {
				let token = Db._GetStorage('token');
				if (!token) {
					// Db._ShowToast('请先登录', () => {

					// })
					login();
					return false;
					// reject({
					// 	data: {
					// 		msg: '请登录'
					// 	}
					// });
				}
				_data.token = token
			}
			_data.method = data_methods;
		}
		Db._ShowLoading('加载中...')
		ajax_num++;
		uni.request({
			url: url,
			method: method,
			data: data,
			dataType: 'JSON',
			success: (res) => {
				if (res.statusCode == 200) {
					if (res.data.status) {
						resolve(res.data);
					} else {
						// 判断错误信息
						console.log(res);
						console.log(Number(res.data.data));
						switch (Number(res.data.data)) {
							case 14007:
							case 14006:
							case 11001:
								// 用户身份过期
								console.log('用户身份过期');
								Db._ShowToast(res.data.msg, () => {
									login();
								})
								break;
							case 14003 || 14004:
								// method参数1不存在
								Db._ShowToast('method参数错误,请联系客服反馈此问题', () => {
									login();
								})
								break;
							case 10059:
								// 订单金额为零 直接支付成功
								res.data.status = true;
								resolve(res.data);
								break;
							default:
								Db._ShowToast(res.data.msg, ()=> {
									reject(res)
								})
								break;
						}
					}
				} else {
					// reject(res.data);
					// Db._ShowToast(res.msg)
					Db._ShowToast('服务器正在加载中,请稍后重试')
				}
			},
			fail: (err) => {
				// Db._ShowToast(res.msg)
				Db._ShowToast('服务器正在加载中,请稍后重试')
			},
			complete: res => {
				// uni.showModal({
				// 	title: JSON.stringify(res),
				// })
				// 加点延时 不然在回调后马上请求的情况下会显得很卡
				setTimeout(() => {
					ajax_num--;
					if (ajax_num <= 0) {
						Db._HideLoading()
					}
				}, 100)
			}
		})
	})
}

// 登录
let is_login = false; // 限制一秒之内只能调用一次
const login = () => {
	if (is_login) {
		return false;
	}
	console.log('login');
	userLogin()
		.then(res => {
			console.log(res);
			location.href = res.data.url
		})

	// 防止多次登录
	setTimeout(() => {
		is_login = false;
	}, 1000)
}

const methodsToken = [
	'cycle.cyclePurchase',
	'cycle.cycleInfo',
	'user.getusership',
	'user.vuesaveusership',
	'user.editShip',
	'user.getAddressInfo',
	'user.removeship',
	'user.editship',
	'cycle.special',
	'cycle.demand',
	'cycle.lowerOrder',
	'cycle.menus',
	'cycle.getCouponcards',
	'cycle.allCycleorder',
	'cycle.evaluate',
	'cycle.oneCycliceInfo',
	'cycle.allCycledish',
	'cycle.dishOrders',
	'cycle.updateAddress',
	'cycle.timeDelay',
	'cycle.requestCoupon',
	'cycle.couponInfo',
	'cycle.dishInfo',
	'cycle.cart',
	'user.getWxInfo',
	'user.pay',
	'user.getDefaultAddr',
	'cycle.service',
	'coupon.getcoupons',
	'coupon.getInfo',
	'cycle.updateOrderstatus',
	'cycle.getYm',
	'cycle.getYmd',
	'cycle.getToken',
	'cycle.newUserCoupons',
	'cycle.getWeight',
	'cycle.six',
	'cycle.getNewInfo',
	'cycle.getNew',
	'cycle.getPay',
	'cycle.editYmd',
	
]


// 用户登录
export const userLogin = () => ajax(apiBaseUrl, 'POST', {
	_callback_url: Db._GetStorage('work_mode')
}, 'user.wxlogin')

// 周期购首页接口
export const cyclePurchase = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.cyclePurchase')

// 获取周期购首页详情接口
export const cycleInfo = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.cycleInfo')

// 下单请求优惠券接口
export const requestCoupon = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.requestCoupon')

// 周期购下单
export const lowerOrder = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.lowerOrder')

// 用户收货地址列表接口
export const getAddrList = () => ajax(apiBaseUrl, 'POST', {}, 'user.getusership')

// 用户获取地址详情
export const getAddressInfo = (data) => ajax(apiBaseUrl, 'POST', data, 'user.getAddressInfo')

// 用户添加收货地址接口
export const addAddr = (data) => ajax(apiBaseUrl, 'POST', data, 'user.vuesaveusership')

// 用户编辑收货地址接口
export const editAddr = (data) => ajax(apiBaseUrl, 'POST', data, 'user.editship')

// 用户删除收货地址接口
export const removeAddr = (data) => ajax(apiBaseUrl, 'POST', data, 'user.removeship')

// 特殊食材接口
export const special = () => ajax(apiBaseUrl, 'POST', {}, 'cycle.special')

// 客户下单需求
export const orderDemand = () => ajax(apiBaseUrl, 'POST', {}, 'cycle.demand')

// 请求菜单接口
export const getOrderMenus = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.menus')

// 请求菜单接口
export const getCouponCards = () => ajax(apiBaseUrl, 'POST', {}, 'cycle.getCouponcards')

// 周期购所有订单接口
export const getAllCycleOrder = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.allCycleorder')

// 周期购/菜品接口 评价
export const evaluate = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.evaluate')

// 周期购订单详情
export const getCycliceOrderDetail = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.oneCycliceInfo')

// 生成菜品订单列表接口
export const dishOrders = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.dishOrders')

// 菜品订单列表接口
export const getCycliceDish = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.allCycledish')

// 菜品订单列表接口
export const updateAddress = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.updateAddress')

// 菜品顺延时间接口
export const timeDelay = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.timeDelay')

// 优惠券id获取优惠券详情接口
export const getCouponInfo = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.couponInfo')

// 菜品id获取菜品详情接口
export const getDishInfo = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.dishInfo')

// 尝鲜券下单
export const foodOrder = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.cart')

// 获取用户信息接口
export const getWxInfo = () => ajax(apiBaseUrl, 'POST', {}, 'user.getWxInfo')

// 支付
export const wxPay = (data) => ajax(apiBaseUrl, 'POST', {
	payment_code: 'wechatpay',
	payment_type: 1,
	status: 'cmf',
	...data
}, 'user.pay')

// 获取用户默认地址
export const getUserDefaultAddr = () => ajax(apiBaseUrl, 'POST', {}, 'user.getDefaultAddr')

// 我的客服富文本
export const getCustomerService = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.service')

// 通过优惠券id领取优惠券
export const getCoupons = (data) => ajax(apiBaseUrl, 'POST', data, 'coupon.getcoupons')

// 通过阅读文章查看优惠券详情
export const getInfo = (data) => ajax(apiBaseUrl, 'POST', data, 'coupon.getInfo')

// 通过阅读文章查看优惠券详情
export const updateOrderStatus = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.updateOrderstatus')

// 通过年月获取当月吃饭的天数集合
export const getFoodDayArrayByYM = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.getYm')

// 通过ymd获取当天订单信息
export const getFoodByYMB = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.getYmd')

// 通过token获取用户id
export const getUserId = () => ajax(apiBaseUrl, 'POST', {}, 'cycle.getToken')

// 新用户通过分享领取周体验劵
export const newUserCoupons = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.newUserCoupons')

// 体重记录
export const getWeight = () => ajax(apiBaseUrl, 'POST', {}, 'cycle.getWeight')

// 体重记录
export const comment = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.six')

// 获取是否有新人优惠券
export const getNewInfo = () => ajax(apiBaseUrl, 'POST', {}, 'cycle.getNewInfo')

// 获取是否有新人优惠券
export const getNew = () => ajax(apiBaseUrl, 'POST', {}, 'cycle.getNew')

// 判断是否付费用户
export const getIsPayUser = () => ajax(apiBaseUrl, 'POST', {}, 'cycle.getPay')

// 判断是否付费用户
export const setEditYmd = (data) => ajax(apiBaseUrl, 'POST', data, 'cycle.editYmd')